﻿using AD域工具.Tool;
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Tool;

namespace AD域工具
{
    #region 主窗口类
    /// <summary>
    /// 主窗口类
    /// </summary>
    #endregion
    public partial class Form1 : Form
    {
        #region 域名地址
        /// <summary>
        /// 域名地址
        /// </summary>
        #endregion
        private string Domain {
            get
            {
                string value = this.input_domain.Text;
                if (value == null || value.Length == 0)
                {
                    throw new Exception("AD域地址,不能为空!");
                }
                return value;
            }
        }

        #region 管理用户
        /// <summary>
        /// 管理用户
        /// </summary>
        #endregion
        private string Username
        {
            get
            {
                string value = this.input_username.Text;
                if (value == null || value.Length == 0)
                {
                    throw new Exception("管理用户,不能为空!");
                }
                return value;
            }
        }

        #region 管理密码
        /// <summary>
        /// 管理密码
        /// </summary>
        #endregion
        private string Password
        {
            get
            {
                string value = this.input_password.Text;
                if (value == null || value.Length == 0)
                {
                    throw new Exception("管理密码,不能为空!");
                }
                return value;
            }
        }

        #region 使用用户
        /// <summary>
        /// 使用用户
        /// </summary>
        #endregion
        private string Handuser
        {
            get
            {
                string value = this.input_handuser.Text;
                if (value == null || value.Length == 0)
                {
                    throw new Exception("使用用户,不能为空!");
                }
                return value;
            }
        }

        #region 中心用户
        /// <summary>
        /// 中心用户
        /// </summary>
        #endregion
        private string CenterName
        {
            get
            {
                string value = this.input_center_user.Text;
                if (value == null || value.Length == 0)
                {
                    throw new Exception("中心用户,不能为空!");
                }
                return value;
            }
        }

        #region 计算机域名称
        /// <summary>
        /// 计算机域名称
        /// </summary>
        #endregion
        private string ComputerName
        {
            get
            {
                string value = this.input_computer_name.Text;
                if (value == null || value.Length == 0)
                {
                    throw new Exception("计算机域名称,不能为空!");
                }
                if (!Regex.IsMatch(value, @"^(ppc|PPC)\d{12}$"))
                {
                    throw new Exception("计算机域名称,不符合规范!");
                }
                return value;
            }
        }

        #region 窗口_初始化
        /// <summary>
        /// 窗口_初始化
        /// </summary>
        #endregion
        public Form1()
        {
            // 初始化窗口组件
            InitializeComponent();
        }

        #region 初始化输入框配置
        /// <summary>
        /// 初始化输入框配置
        /// </summary>
        #endregion
        private void Init_InputConfig() 
        {
            AppConfig.BindInput(this.input_domain, "ad_domain");
            AppConfig.BindInput(this.input_username, "username");
            AppConfig.BindInput(this.input_center_user, "center_user");
        }

        #region 窗口_加载中
        /// <summary>
        /// 窗口_加载中
        /// </summary>
        /// <param name="sender">触发元素</param>
        /// <param name="e">时间信息</param>
        #endregion
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Init_InputConfig();
        }

        #region 打印日志
        /// <summary>
        /// 打印日志
        /// </summary>
        /// <param name="message">要显示的日志信息</param>
        #endregion
        private void Log(string message) {
            base.Text = $"AD域工具：{message}";
        }

        #region 按钮_按下_检查使用用户是否在AD域
        /// <summary>
        /// 按钮_按下_检查使用用户是否在AD域
        /// </summary>
        /// <param name="sender">触发元素</param>
        /// <param name="e">事件信息</param>
        #endregion
        private void Button_check_user_Click(object sender, EventArgs e)
        {
            try
            {
                string organization = ADDomain.GetUser(this.Domain, this.Username, this.Password, this.Handuser);
                if (organization == null)
                {
                    throw new Exception("AD域无此用户!");
                }
                else
                {
                    Log("AD域有用户!");
                }
            }
            catch (Exception error)
            {
                Log(error.Message);
                MessageBox.Show(error.Message);
            }
        }

        #region 按钮_按下_修改计算机名称
        /// <summary>
        /// 按钮_按下_修改计算机名称
        /// </summary>
        /// <param name="sender">触发元素</param>
        /// <param name="e">事件信息</param>
        #endregion
        private void Button_edit_computer_name_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ComputerHelper.IsRunAsAdmin())
                {
                    throw new Exception("请使用管理员身份打开软件！");
                }
                ComputerHelper.SetComputerNameByRegedit(this.ComputerName);
                Log("修改计算机名称成功");
            }
            catch (Exception error)
            {
                Log(error.Message);
                MessageBox.Show(error.Message);
            }
        }

        #region 按钮_按下_计算机加入AD域
        /// <summary>
        /// 按钮_按下_计算机加入AD域
        /// </summary>
        /// <param name="sender">触发元素</param>
        /// <param name="e">事件信息</param>
        #endregion
        private void Button_join_doamin_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ComputerHelper.IsRunAsAdmin())
                {
                    throw new Exception("请使用管理员身份打开软件！");
                }
                string error = API.NetJoinDomain(this.Domain, this.Username, this.Password);
                if (error == null)
                {
                    Log("加域成功");
                }
                else
                {
                    throw new Exception(error);
                }
            }
            catch (Exception error)
            {
                Log(error.Message);
                MessageBox.Show(error.Message);
            }
        }

        #region 按钮_按下_中心用户加至计算机管理员组
        /// <summary>
        /// 按钮_按下_中心用户加至计算机管理员组
        /// </summary>
        /// <param name="sender">触发元素</param>
        /// <param name="e">事件信息</param>
        #endregion
        private void Button_center_join_admin_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ComputerHelper.IsRunAsAdmin())
                {
                    throw new Exception("请使用管理员身份打开软件！");
                }
                string error1 = ADDomain.JoinDomainUserToLocalGroup("Administrators", this.Domain, this.CenterName);
                if (error1 == null)
                {
                    Log("设置中心用户为计算机管理员成功!");
                }
                else
                {
                    throw new Exception("设置中心用户为计算机管理员失败：" + error1);
                }
            }
            catch (Exception error)
            {
                Log(error.Message);
                MessageBox.Show(error.Message);
            }
        }

        #region 按钮_按下_使用用户加至计算机管理员组
        /// <summary>
        /// 按钮_按下_使用用户加至计算机管理员组
        /// </summary>
        /// <param name="sender">触发元素</param>
        /// <param name="e">事件信息</param>
        #endregion
        private void Button_user_join_admin_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ComputerHelper.IsRunAsAdmin())
                {
                    throw new Exception("请使用管理员身份打开软件！");
                }
                string error = ADDomain.JoinDomainUserToLocalGroup("Administrators", this.Domain, this.Handuser);
                if (error == null)
                {
                    Log("设置使用用户为计算机管理员成功!");
                }
                else
                {
                    throw new Exception("设置使用用户为计算机管理员失败：" + error);
                }
            }
            catch (Exception error)
            {
                Log(error.Message);
                MessageBox.Show(error.Message);
            }
        }

        #region 按钮_按下_打开激活工具
        /// <summary>
        /// 按钮_按下_打开激活工具
        /// </summary>
        /// <param name="sender">触发元素</param>
        /// <param name="e">事件信息</param>
        #endregion
        private void Button_open_activation_Click(object sender, EventArgs e)
        {
            try
            {
                string path = ComputerHelper.RunPath + "\\激活工具";
                if (!Directory.Exists(path))
                {
                    throw new Exception("未找到激活工具目录");
                }
                ComputerHelper.OpenDirInExplorer(path);
            }
            catch (Exception error)
            {
                Log(error.Message);
                MessageBox.Show(error.Message);
            }
        }

        #region 按钮_按下_打开加域工具
        /// <summary>
        /// 按钮_按下_打开加域工具
        /// </summary>
        /// <param name="sender">触发元素</param>
        /// <param name="e">事件信息</param>
        #endregion
        private void Button_open_join_doamin_Click(object sender, EventArgs e)
        {
            try
            {
                string path = ComputerHelper.RunPath + "\\加域工具";
                if (!Directory.Exists(path))
                {
                    throw new Exception("未找到加域工具目录");
                }
                ComputerHelper.OpenDirInExplorer(path);
            }
            catch (Exception error)
            {
                Log(error.Message);
                MessageBox.Show(error.Message);
            }
        }

        #region 按钮_按下_为当前用户目录添加使用用户权限
        /// <summary>
        /// 按钮_按下_为当前用户目录添加使用用户权限
        /// </summary>
        /// <param name="sender">触发元素</param>
        /// <param name="e">事件信息</param>
        #endregion
        private void Button_add_userfolder_control_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ComputerHelper.IsRunAsAdmin())
                {
                    throw new Exception("请使用管理员身份打开软件！");
                }
                FolderPermission.AddFolderFullControl(ComputerHelper.GetUserFolder(), this.Handuser);
            }
            catch (Exception error)
            {
                Log(error.Message);
                MessageBox.Show(error.Message);
            }
        }
    }
}
